﻿using System;
using System.Web;

namespace Noria.Web.Utility
{
    internal class ErrorHandler
    {
        // Create a logger for use in this class
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public void Process(HttpResponse response, Exception error)
        {
            // don't know why error could be null
            error = error ?? new ServerException("Unknown error");

            log.Error("Application_Error", error);

            var e = error as NHttpException ?? new ServerException(error.Message);

            // clear
            response.ClearHeaders();
            response.Clear();
            response.ClearContent();

            response.ContentType = "application/json";

            // skipping IIS custom status code handling...
            response.TrySkipIisCustomErrors = true;
            response.StatusCode = e.StatusCode;

            response.Write(e.Stringify());
            response.End();
        }


    }
}